package com.example.androiddemo.day714;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.androiddemo.R;

public class MusicService extends Service {

    private MusicBinder mBinder;
    private MediaPlayer mMediaPlayer;

    public class MusicBinder extends Binder {
        public void startMusic() {
            Log.i("======================", "Start Play Music By BindService.");
            if (mMediaPlayer != null) {
                mMediaPlayer.start();
            }
        }

        public void stopMusic() {
            Log.i("=======================", "Stop Play Music By BindService.");
            if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayer = MediaPlayer.create(this, R.raw.music);
        mBinder = new MusicBinder();
        Log.i("=====================", "Music Service onCreate.");
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        /**
         *  播放器通过绑定的方式来控制音乐播放，通过启动的方式启动服务。
         *  启动方式仅仅用来启动服务，因此不需要在该函数中设置有关音乐播放的逻辑。
         *  只需要控制通知栏的启动和关闭即可。
         */
        Log.i("=====================","服务通过启动方式启动");
        int cmd = intent.getExtras().getInt("cmd");
        if (cmd == 0) {
            createNotification(this);
//            if (mMediaPlayer != null) {
//                if (mMediaPlayer.isPlaying()) {
//                    Log.i("==================", "Stop Play Music By Start Mode");
//                    mMediaPlayer.pause();
//                } else {
//                    Log.i("==================", "Start Play Music By Start Mode");
//                    mMediaPlayer.start();
//                }
//            }
        } else {
            stopForeground(true);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 创建通知栏
     *
     * @param context
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotification(Context context) {
        String chanelId = "Music Chanel ID";
        String chanelName = "Music Chanel";

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(chanelId, chanelName, NotificationManager.IMPORTANCE_HIGH);
        channel.enableLights(true); //显示通知灯
        channel.setLightColor(Color.RED);//设置通知灯为红色
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        manager.createNotificationChannel(channel);

        Intent intent = new Intent(this, ServiceActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new Notification.Builder(this, chanelId)
                .setChannelId(chanelId)
                .setContentTitle("音乐服务")
                .setWhen(System.currentTimeMillis())
                .setContentText("正在播放音乐")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
    }

    @Override
    public void onDestroy() {
        Log.i("===============","服务被销毁");
        super.onDestroy();
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
        }
    }


}
